//! oasis-core/src/agent_types.rs - 统一的Agent相关类型

use crate::core_types::AgentId;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentInfo {
    pub agent_id: AgentId,
    pub status: AgentStatus,
    pub info: HashMap<String, String>, // 合并facts和labels和 groups
    pub last_heartbeat: i64,
    pub version: String,
    pub capabilities: Vec<String>,
}

impl AgentInfo {
    pub fn new(agent_id: AgentId) -> Self {
        Self {
            agent_id,
            status: AgentStatus::Offline,
            info: HashMap::new(),
            last_heartbeat: 0,
            version: String::new(),
            capabilities: Vec::new(),
        }
    }

    pub fn online(mut self) -> Self {
        self.status = AgentStatus::Online;
        self.last_heartbeat = chrono::Utc::now().timestamp();
        self
    }

    pub fn with_info(mut self, key: String, value: String) -> Self {
        self.info.insert(key, value);
        self
    }

    pub fn with_capability(mut self, capability: String) -> Self {
        self.capabilities.push(capability);
        self
    }
}

/// 强类型的Agent状态
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AgentStatus {
    Online,  // 在线（心跳正常）
    Offline, // 离线（心跳超时）
    Removed, // 移除（手动移除）
    Unknown,
}

impl AgentStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Online => "online",
            Self::Offline => "offline",
            Self::Removed => "removed",
            Self::Unknown => "unknown",
        }
    }
}

impl From<String> for AgentStatus {
    fn from(s: String) -> Self {
        match s.to_lowercase().as_str() {
            "online" => Self::Online,
            "offline" => Self::Offline,
            "removed" => Self::Removed,
            _ => Self::Unknown,
        }
    }
}
